<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.ArrayList"%>
<%@page import="bean.AuctionItemBean"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>	
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	// determines if the bid button should be shown
	boolean owner = false;
	boolean allowedToBid = true;
	int userID = (Integer) request.getSession().getAttribute("userID");
	AuctionItemBean AIB = (AuctionItemBean) request.getAttribute("goToBean");
	if(AIB.getUserID() == userID){
		allowedToBid = false;
		owner = true;
	}
	if(AIB.getTopBid().getTime() != null){
		if(  AIB.getTopBid().getBidUserID() == userID){
			allowedToBid = false;
		}
		if(new Date().getTime() >= AIB.getEndTime()) {
			allowedToBid = false;
		}
	}
	
	// Turns the bid text box red after an invalid number
	String errorColor = "border-color: red;";
	String numberNotValidErr = "";
	
	String error = request.getParameter("error");
	if(error != null) {
		if(error.equals("numberNotValidError")) { numberNotValidErr = errorColor; }
	}
	
	// Set the bid value
	String bidTextboxValue = request.getParameter("bidValue");
	if(bidTextboxValue == null){
		bidTextboxValue = "";
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<input type="hidden" id="countdown" value="false"/>

<c:set var="now" value="<%=(new Date()).getTime()%>"/>
<c:set var="timeLeft" value="${(goToBean.endTime-now)/1000}"/>
<c:set var="owner" value="<%=owner %>"/>
<c:set var="allowedToBid" value="<%=allowedToBid %>"/>
<c:set var= "userID" value="<%=request.getSession().getAttribute(\"userID\")%>"/>
<c:if test="${(timeLeft < 0)}">
	<c:set var="timeLeft" value="0"/>
</c:if>

<div class="item" id="item">
	<div class="item-pic" id="item-pic"></div>
	<div id="item-details">
		<div class="item-name" id="item-name">
			<h1>
				${goToBean.title}
			</h1>
		</div>
		<div id="item-time" class="item-time"><input type="hidden" class="item-to-time" value="${timeLeft}"/></div>
		<c:choose>
			<c:when test="${goToBean.topBid.time != null}">
				<c:choose>
					<c:when test="${goToBean.topBid.bidUserID == userID}">
						<div class="item-price">&euro;${goToBean.topBid.bidValue},- (your bid)</div>
					</c:when>
					<c:otherwise>
						<c:choose>
							<c:when test="${goToBean.yourTopBid == null }">
								<div class="item-price">&euro;${goToBean.topBid.bidValue},- </div>
							</c:when>
							<c:otherwise>
								<div class="item-price">&euro;${goToBean.topBid.bidValue},- (&euro;${goToBean.yourTopBid.bidValue},- your bid )</div>
							</c:otherwise>
						</c:choose>
					</c:otherwise>
				</c:choose>
			</c:when>
			<c:otherwise>
				<div class="item-price">&euro;${goToBean.startPrice},-</div>
			</c:otherwise>
		</c:choose>
		<div id="item-owner">${goToBean.owner}</div>
		<c:choose>
			<c:when test="${allowedToBid == true }">
				<div id="item-bid">
					<input type="submit" class="button" id="button" value="Bid" onclick="doBid(${goToBean.auctionItemID})"/>
					<input style="<%=numberNotValidErr%>" type="text" id="bid" value="<%=bidTextboxValue%>"/>	
				</div>
			</c:when>
		</c:choose>
		<div class="item-desc" id="item-desc">
			<p>${goToBean.description}</p>
		</div>
		<c:choose>
			<c:when test="${owner == true }">
				<div id="item-bids">
					<table>
						<c:forEach var='bid' items="${goToBean.bids}">
							<tr><td>${bid.userName}</td><td>&euro;${bid.bidValue}</td></tr>
						</c:forEach>
					</table>
				</div>
			</c:when>
		</c:choose>
	</div>
</div>